import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('GridView'),
        ),
        body: HomeContent(),
      ),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

//body内容组件
class HomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GridView.count(
      children: <Widget>[
        Container(
          alignment: Alignment.center,
          child: Text("data1"),
          color: Colors.blue,
        ),
        Container(
          child: Text("data1"),
          color: Colors.yellow,
        ),
        Container(
          alignment: Alignment.center,
          child: Text("data1"),
          color: Colors.blue,
        ),
        Container(
          child: Text("data1"),
          color: Colors.yellow,
        ),
        Container(
          alignment: Alignment.center,
          child: Text("data1"),
          color: Colors.blue,
        ),
        Container(
          child: Text("data1"),
          color: Colors.yellow,
        ),
      ],
      crossAxisCount: 3, // 一行显示个数
      crossAxisSpacing: 20.0,
      mainAxisSpacing: 20.0,
      childAspectRatio: 0.7, //子组件宽高比
      padding: EdgeInsets.all(10),
    );
  }
}
